package lookMessage;

import Bean.CheckBean;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.JOptionPane;
import loginRegister.LoginBean;
import Bean.SqlBean;


/**
 *
 * @author 刘杉
 */
public class UpdateMessageServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws  IOException {
        String phone=new String(request.getParameter("phone").getBytes("ISO-8859-1"),"UTF-8");
        String place=new String(request.getParameter("place").getBytes("ISO-8859-1"),"UTF-8");
        String email=new String(request.getParameter("email").getBytes("ISO-8859-1"),"UTF-8");
        String userName="";
        HttpSession session=request.getSession();
        LoginBean login=(LoginBean)session.getAttribute("login");
        if(login==null){
            response.sendRedirect("http://localhost:8080/login.jsp");
        }
        else if(!CheckBean.isNumeric(phone)){
            CheckBean.alert("联系电话由数字组成！");
            response.sendRedirect("http://localhost:8080/lookMessage/updateMessage.jsp");
        }else{
            userName=login.getUserName();
            try{
                ResultSet rs = null;
                SqlBean sb = new SqlBean();
                sb.connectSQL();
                String sql1="Update user set phone='"+phone+"',place='"+place+"',email='"+email+"' where userName='"+userName+"'";
                sb.executeUpdate(sql1);
                String sql2="select * from user where userName='"+userName+"'";
                rs=sb.executeQuery(sql2);
                LookMessageBean mm=new LookMessageBean();
                while(rs.next()){
                    mm.setName(rs.getString("name"));
                    mm.setSex(rs.getString("sex"));
                    mm.setIdCard(rs.getString("idCard"));
                    mm.setBirth(rs.getString("birth"));
                    mm.setNation(rs.getString("nation"));
                    mm.setEdu(rs.getString("edu"));
                    mm.setWork(rs.getString("work"));
                    mm.setMonthPay(rs.getString("monthPay"));
                    mm.setPhone(rs.getString("phone"));
                    mm.setPlace(rs.getString("place"));
                    mm.setEmail(rs.getString("email"));
                    mm.setLevel(rs.getString("level"));
                    mm.setPicture(rs.getString("picture"));
                    mm.setDgut_username(rs.getString("dgut_username"));
                }
                session.setAttribute("wordlist", mm);
                sb.closeSQL();
                CheckBean.alert("填写信息合格，修改成功！");
                response.sendRedirect("http://localhost:8080/lookMessage/updateMessage.jsp");
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        doGet(request, response);
    }

}
